﻿$(function () {
    $("#btnSendCheckNum").live("click", btnSendCheckNumClick);
    $("#btnRegister").live("click", btnRegisterClick);
    $("#loginInRegister").live("click", EnteWayClick);//注册页面登录
    
    /******************函数******************/
    //注册
    function btnRegisterClick() {
        if (!formCheck())
        {
            return;
        }
        var data = $("#formRegister").serialize();
        $.ajax({
            url: "../User/Register",
            data: data,
            dataType: "json",
            beforeSend: function () {

            },
            type: "post",
            success: function (result) {
                if (result.Result.IsSuccess) {
                    alert("注册成功");
                    location.href = "../Home";
                }
                else {
                    alert(result.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }

    function formCheck()
    {
        var emialExp =  /\w@\w*\.\w/;
        var mobileExp = /^(1[0-9]{10})$/;
        var res = true;
        if ($("#checkRuler").attr("checked") !== "checked")
        {
            alert("请勾选我已阅读《直通出国快车使用协议》");
            $("#checkRuler").focus();
            res = false;
            return res;
        }
        var Mobile = $("#Mobile").val();
        if (!mobileExp.test(Mobile))
        {
            alert("请输入正确格式的手机号码");
            $("#Mobile").focus();
            res = false;
            return res;
        }
        var CheckNum = $("#CheckNum").val();
        if (CheckNum == "")
        {
            alert("验证码不得为空");
            $("#CheckNum").focus();
            res = false;
            return res;
        }
        var UserName = $("#UserName").val();
        if (UserName == "") {
            alert("姓名不得为空");
            $("#UserName").focus();
            res = false;
            return res;
        }
        var Email = $("#Email").val();
        if (!emialExp.test(Email)) {
            alert("请输入正确格式的邮箱");
            $("#Email").focus();
            res = false;
            return res;
        }

        var Password = $("#Password").val();
        var PasswordDoubleCheck = $("#PasswordDoubleCheck").val();

        if (Password == "" || PasswordDoubleCheck == "" || Password != PasswordDoubleCheck) {
            alert("两次密码输入不一致");
            $("#Password").focus();
            res = false;
            return res;
        }

        if (!(Password.length >= 6 && Password.length <= 12))
        {
            alert("请输入长度为6到12位的密码");
            $("#Password").focus();
            res = false;
            return res;
        }
        return res;
    }
    //发送验证码
    function btnSendCheckNumClick()
    {
        var mobile = $("#Mobile").val();
        if (mobile=="")
        {
            alert("手机号码不得为空");
            return;
        }
        sendCheckNum(mobile);
    }
    //验证码发送
    function sendCheckNum(mobile)
    {
        //
        $.ajax({
            url: "../User/SendCheckNum",
            dataType: "json",
            data: { mobile: mobile },
            beforeSend: function () {

            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("发送成功");
                    //alert("验证码" + res.Result.Message);
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {

            }
        });
        //
    }
});